import { useAuthStore } from "@/store/modules/auth";
import type { RouteRecordRaw } from "vue-router";
import router from "@/router";

const modules = import.meta.glob("@/views/**/*.vue");

/**
 * @description 初始化动态路由
 */

export const initDynamicRouter = async () => {
  const authStore = useAuthStore();
  try {
    if (authStore.isLoaded) return;

    await authStore.getAuthMenuList();

    await authStore.setLoaded();

    authStore.flatMenuListGet.forEach((item) => {
      item.children && delete item.children;
      if (item.component && typeof item.component == "string") {
        item.component = modules["/src/views" + item.component + ".vue"];
      }
      if (item.meta.isFull === "T") {
        router.addRoute(item as unknown as RouteRecordRaw);
      } else {
        router.addRoute("layout", item as unknown as RouteRecordRaw);
      }
    });
  } catch (error) {}
};
